//
//  UITextView+HBExtension.swift
//  sinaWeibo
//
//  Created by Beans on 16/7/3.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

extension UITextView {

    func getPublishTextString() -> String {

        /** 遍历textView.attributedText
        range: 要遍历的字符串range
        options: 选一个默认值
        block
        */

        var publishString = String()
        self.attributedText.enumerateAttributesInRange(NSMakeRange(0, self.attributedText.length), options: []) { (data, range, _) -> Void in
//            print(data)
//            print(range)

            if let attachment = data["NSAttachment"] as? HBTextAttachment {
//                printLog(attachment.emoticon?.chs)

                // 拼接所要发布表情的文字形式 e.g.`[嘻嘻]`
                if let chs = attachment.emotion?.chs {
                    publishString = publishString + chs
                }
            } else {
                // 拼接字符串(含emoji)
                let subString = (self.text as NSString).substringWithRange(range)
                publishString = publishString + subString
            }
        }
        printLog(publishString)

        return publishString
    }

    func insertEmotion(oEmotion: HBEmotion) {
        // 表情图片
        if let _ = oEmotion.png {
            // 之前的表情
            let muAttribute = NSMutableAttributedString(attributedString: self.attributedText)

            /*
            // 1. 创建附件对象
            let attachment = NSTextAttachment()
            // 1.1 拼接路径
            guard let path = oEmotion.path, png = oEmotion.png else {
                return
            }
            let imagePath = "\(path)/\(png)"
            // 1.2 设置属性
            attachment.image = UIImage(named: imagePath)
            // 1.3 修改图片bounds
            let lineHeight = self.font?.lineHeight
            attachment.bounds = CGRectMake(0, -5, lineHeight!, lineHeight!)  // y的负值向下

            // 2. NSAttributeString对象
            let attribute = NSAttributedString(attachment: attachment)
            */

//            // (对象方法)
//            let attachment = HBTextAttachment()
//            // 一定要记得传递表情
//            attachment.emotion = oEmotion
//            let attribute = attachment.getAttributeString(self.font!)

            // (类方法)
            let attribute = HBTextAttachment.getAttributeString(oEmotion, font: self.font!)

            // 3. 拼接
            let range = self.selectedRange
            if let attribute = attribute {
//                muAttribute.appendAttributedString(attribute)  // 末尾, 不能中间插入
//                muAttribute.insertAttributedString(attribute, atIndex: Int)  // index不好获取
                // 替换图片表情
                muAttribute.replaceCharactersInRange(range, withAttributedString: attribute)
            }

            // 3.1 需要改muAttribute的属性
            muAttribute.addAttributes([NSFontAttributeName : self.font!], range: NSMakeRange(0, muAttribute.length))

            // 4. 赋值
            self.attributedText = muAttribute
            // 赋值之后恢复光标位置
            self.selectedRange = NSMakeRange(range.location + 1, 0)

            // 隐藏占位文字
            // ①时间点 ②调用代理方法
            self.delegate?.textViewDidChange!(self)
        }

        // emoji图片
        if let emoji = oEmotion.emoji {
            self.replaceRange(self.selectedTextRange!, withText: emoji)  // selectedTextRange
        }
    }

}